package com.leoren.run.utils;

import com.amap.api.services.route.DriveRouteResult;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.leoren.run.common.ResponseCode;
import com.leoren.run.common.ServerResponse;
import com.leoren.run.log.L;
import com.leoren.run.vo.AnnounceVo;
import com.leoren.run.vo.CommentVo;
import com.leoren.run.vo.MapRouteVo;
import com.leoren.run.vo.RunMapRouteVo;
import com.leoren.run.vo.ShareDataVo;
import com.leoren.run.vo.User;
import com.leoren.run.vo.UserRunRecordVo;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

/**
 * @Author Leoren
 * @Date 2019/1/14 16:00
 *
 * Gson  解析
 */
public class GsonUtil {

    private static final String TAG = "GsonUtil";

    private static Gson gson = new Gson();

    public static String formatBeanToJSON(Object obj){
        return gson.toJson(obj);
    }

    public static ServerResponse formatJsonToResponseUser(String jsonData){
        ServerResponse<User> response = gson.fromJson(jsonData, new TypeToken<ServerResponse<User>>(){}.getType());
        return  response;
    }

    public static ServerResponse<List<MapRouteVo>> formatJsonToResponseMapRouteList(String jsonData){
        System.out.println(jsonData);
        ServerResponse<List<MapRouteVo>> response = null;
        try {
            JSONObject obj = new JSONObject(jsonData);
            int status = obj.optInt("status");
            String msg = obj.optString("msg");
            if(status != ResponseCode.SUCCESS.getCode()){
                response = new ServerResponse(status, msg);
            }else {
                String dataJsonStr = obj.optString("data");
                List<MapRouteVo> addressVoList = gson.fromJson(dataJsonStr, new TypeToken<List<MapRouteVo>>(){}.getType());
                response = new ServerResponse<>(status, msg, addressVoList);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return response;
    }

    public static <T> List<T> formatJsonToVoList(String jsonDate) {
        Type type = new TypeToken<List<T>>(){}.getType();
        List<T> list = gson.fromJson(jsonDate, type);
        return list;
    }

    public static String GenerateJsonStr(Object object){
        String json = gson.toJson(object);
        L.e(json);
        return json;
    }

    public static ServerResponse<RunMapRouteVo> formatJsonToMapRouteVo(String responseStr) {
        ServerResponse<RunMapRouteVo> response = gson.fromJson(responseStr, new TypeToken<ServerResponse<RunMapRouteVo>>(){}.getType());
        return  response;
    }

    public static ServerResponse formatJsonToServerResponse(String responseStr) {
        ServerResponse response = gson.fromJson(responseStr, new TypeToken<ServerResponse>(){}.getType());
        return  response;
    }

    public static ServerResponse<List<CommentVo>> formatJsonToCommentListVo(String jsonData) {
        System.out.println(jsonData);
        ServerResponse<List<CommentVo>> response = null;
        try {
            JSONObject obj = new JSONObject(jsonData);
            int status = obj.optInt("status");
            String msg = obj.optString("msg");
            if(status != ResponseCode.SUCCESS.getCode()){
                response = new ServerResponse(status, msg);
            }else {
                String dataJsonStr = obj.optString("data");
                List<CommentVo> addressVoList = gson.fromJson(dataJsonStr, new TypeToken<List<CommentVo>>(){}.getType());
                response = new ServerResponse<>(status, msg, addressVoList);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return response;

    }

    public static List<CommentVo> commentList(String json){
        List<CommentVo> addressVoList = gson.fromJson(json, new TypeToken<List<CommentVo>>(){}.getType());
        return addressVoList;
    }

    public static void main(String[] args) {
        String str = "{\"status\":0,\"msg\":\"查找成功\",\"data\":[{\"id\":1,\"userId\":4,\"content\":\"哈哈哈哈\",\"picUrl\":\"http://192.168.1.100:20088/file/b18020d1-f9fd-4c61-a99b-9244a1ec3d44.png\",\"likeUserList\":\"\",\"time\":1580908373000,\"userName\":\"leoren\",\"userPicUrl\":\"http://192.168.1.100:20088/user/39c0dfedc1869e03ea0bed8d99ffc0b4.jpg\",\"dataTimeStr\":\"2020-02-05  21:12:53\",\"likeUsers\":[],\"isLike\":0,\"commentReplays\":[]}]}" ;
        List<CommentVo> list = commentList(str);
        System.out.println(list);
    }

    public static String formatBeanToRouteResult(DriveRouteResult result) {
        String str = gson.toJson(result);
        return str;
    }


    public static ServerResponse<UserRunRecordVo> formatJsonToUserRunRecordVo(String responseStr) {
        ServerResponse<UserRunRecordVo> response = gson.fromJson(responseStr, new TypeToken<ServerResponse<UserRunRecordVo>>(){}.getType());
        return  response;
    }

    public static ShareDataVo formatJsonToShareDateVo(String runMapStr) {
        ShareDataVo response = gson.fromJson(runMapStr, new TypeToken<ShareDataVo>(){}.getType());
        return  response;
    }

    public static AnnounceVo formatJsonToAnnounceVo(String str) {
        AnnounceVo response = gson.fromJson(str, new TypeToken<AnnounceVo>(){}.getType());
        return  response;
    }

    public static ServerResponse<List<AnnounceVo>> formatJsonToAnnounceVoList(String responseStr) {
        System.out.println(responseStr);
        ServerResponse<List<AnnounceVo>> response = null;
        try {
            JSONObject obj = new JSONObject(responseStr);
            int status = obj.optInt("status");
            String msg = obj.optString("msg");
            if(status != ResponseCode.SUCCESS.getCode()){
                response = new ServerResponse(status, msg);
            }else {
                String dataJsonStr = obj.optString("data");
                List<AnnounceVo> addressVoList = gson.fromJson(dataJsonStr, new TypeToken<List<AnnounceVo>>(){}.getType());
                response = new ServerResponse<>(status, msg, addressVoList);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return response;
    }
}
